home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15832 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  75 lines

  1. Path: ns2.mainstreet.net!ablecom!usenet
  2. From: The Letter O <jczech@ablecom.net>
  3. Newsgroups: comp.lang.c++
  4. Subject: easy c++ question
  5. Date: Mon, 08 Apr 1996 05:08:58 -0700
  6. Organization: HERNK.com
  7. Message-ID: <316901DA.3138C677@ablecom.net>
  8. NNTP-Posting-Host: jczech.ppp.ablecom.net
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i586)
  13.  
  14. Okay, I'm a newbie in C++, and am currently working through
  15. Practical C++ programming by O'Reilly & Associates. I'm working
  16. on my first C++ program with classes, and the objective is to 
  17. make a simple stack class with member functions, a constructor
  18. and a destructor. simple right? 
  19.  
  20. //======================================================
  21. class stack {
  22.     private:
  23.         int count;       // number of items in the stack
  24.         int data[100];   // the items themselves
  25.     public:
  26.     .
  27.     .
  28.     .
  29. //======================================================
  30.  
  31.  
  32. I was using the following init() member function to initialize
  33. my stack variables:
  34.  
  35. //=======================================
  36. inline void stack::init(void)
  37. {
  38.         count = 0;      // zero the stack
  39. }
  40. //=======================================
  41.  
  42. Simple, right? right. Then I modified it and made it into a 
  43. constructor, so that it would automatically be called whenever
  44. I declared a variable of type: stack  as follows:
  45.  
  46. //==============================================
  47. inline stack::stack(void)
  48. {
  49.         count = 0;      // zero the stack
  50.         cout << "constructor has been called\n";
  51. }
  52. //==============================================
  53.  
  54. When I changed it to the constructor (above) it was not called
  55. automatically when I declared a variable of type: stack, and 
  56. thus the stack counter wasn't initialized, and I got a SIGSEGV
  57. signal because count starts out with a junk value that's beyond
  58. the bounds of the data array of my stack variable.
  59.  
  60. My point is: Aren't constructors automagically called when a variable
  61. is declared, simply because they have the same name as the class? 
  62. ie: stack::stack(void)
  63.  
  64.  
  65. I'm using G++ 2.7.0 under Linux slackware 3.0, and Kernel 1.2.13
  66. Is there some compiler switch to enable constructors? Is there a bug in
  67. this version of g++? Am I just a moron who should be banished from programming? 
  68.  
  69. Thanks for any help provided...
  70.  
  71. *Jczech@ablecom.net
  72.  
  73. Replies by email and/or newsgroup are appreciated.
  74.  
  75.